﻿<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Imagin.Controls.Common"
    xmlns:Common.Data.Converters="clr-namespace:Imagin.Common.Data.Converters;assembly=Imagin.Common">
    <Common.Data.Converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
    
    <Style x:Key="{x:Type local:ToggleSwitch}" TargetType="{x:Type local:ToggleSwitch}">
        <Setter Property="BorderThickness" Value="3"/>
        <Setter Property="FontSize" Value="13"/>
        <Setter Property="Padding" Value="2"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:ToggleSwitch}">
                    <ControlTemplate.Resources>
                        <Storyboard x:Key="Storyboard.OnChecking">
                            <DoubleAnimationUsingKeyFrames 
                                BeginTime="00:00:00" 
                                Storyboard.TargetName="PART_Slider"
                                Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
                                <SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="40" />
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                        <Storyboard x:Key="Storyboard.OnUnchecking">
                            <DoubleAnimationUsingKeyFrames 
                                BeginTime="00:00:00" 
                                Storyboard.TargetName="PART_Slider" 
                                Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
                                <SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="0" />
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                    </ControlTemplate.Resources>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <ContentPresenter 
                            Content="{TemplateBinding Content}" 
                            ContentStringFormat="{TemplateBinding ContentStringFormat}" 
                            ContentTemplate="{TemplateBinding ContentTemplate}" 
                            RecognizesAccessKey="True" 
                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                            VerticalAlignment="Center"/>
                        <Grid 
                            Grid.Column="1"
                            Height="27"
                            Width="60">
                            <Border 
                                x:Name="PART_Background" 
                                BorderBrush="{TemplateBinding BorderBrush}" 
                                BorderThickness="{TemplateBinding BorderThickness}" 
                                CornerRadius="{TemplateBinding CornerRadius}" 
                                Padding="{TemplateBinding Padding}">
                                <Grid Background="{TemplateBinding Background}">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition />
                                        <ColumnDefinition />
                                    </Grid.ColumnDefinitions>
                                    <Line 
                                        x:Name="On" 
                                        HorizontalAlignment="Center" 
                                        Stroke="{TemplateBinding Foreground}" 
                                        StrokeThickness="2"  
                                        VerticalAlignment="Center"
                                        Visibility="{TemplateBinding IsChecked, Converter={StaticResource BooleanToVisibilityConverter}}"
                                        X1="0"
                                        X2="0" 
                                        Y1="0" 
                                        Y2="{TemplateBinding FontSize}"/>
                                    <Ellipse 
                                        Grid.Column="1"
                                        x:Name="Off" 
                                        Height="{TemplateBinding FontSize}"
                                        HorizontalAlignment="Center" 
                                        Stroke="{TemplateBinding Foreground}" 
                                        StrokeThickness="2" 
                                        VerticalAlignment="Center"
                                        Visibility="{TemplateBinding IsChecked, Converter={StaticResource BooleanToVisibilityConverter}, ConverterParameter=Inverted}"
                                        Width="{TemplateBinding FontSize}"/>
                                </Grid>
                            </Border>
                            <Thumb
                                x:Name="PART_Slider"
                                Height="{TemplateBinding Height}" 
                                HorizontalAlignment="Left" 
                                RenderTransformOrigin="0.5,0.5"
                                Style="{TemplateBinding ThumbStyle}">
                                <Thumb.RenderTransform>
                                    <TransformGroup>
                                        <ScaleTransform 
                                            ScaleX="1" 
                                            ScaleY="1"/>
                                        <SkewTransform
                                            AngleX="0" 
                                            AngleY="0"/>
                                        <RotateTransform 
                                            Angle="0"/>
                                        <TranslateTransform 
                                            X="0" 
                                            Y="0"/>
                                    </TransformGroup>
                                </Thumb.RenderTransform>
                            </Thumb>
                        </Grid>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="True">
                            <Trigger.EnterActions>
                                <BeginStoryboard Storyboard="{StaticResource Storyboard.OnChecking}"/>
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <BeginStoryboard Storyboard="{StaticResource Storyboard.OnUnchecking}"/>
                            </Trigger.ExitActions>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>